﻿Imports System.Data.SqlClient
Imports System.Data
<Serializable()> Public Class Parametros
    Private _color As ArrayList
    Private _maximo As String
    Private _minimo As String
    Private _id As Integer

    Public Sub New()
        _color = New ArrayList()
        _maximo = ""
        _minimo = ""
        _id = 0
    End Sub
    Property color(ByVal x As Integer) As String
        Get
            If x < 0 Or x >= _color.Count Then
                Return ""
            Else
                Return _color.Item(x)
            End If
        End Get
        Set(ByVal value As String)
            If x >= _color.Count Then
                _color.Add(value)
            Else
                _color.Item(x) = value
            End If
        End Set
    End Property
    Property maximo() As String
        Get
            Return _maximo
        End Get
        Set(ByVal value As String)
            _maximo = value
        End Set
    End Property
    Property minimo() As String
        Get
            Return _minimo
        End Get
        Set(ByVal value As String)
            _minimo = value
        End Set
    End Property
    Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property
End Class

Partial Class frmParametrosGrafico
    Inherits System.Web.UI.Page
    Public Parametros As Parametros
    Public TextBox() As TextBox
    Public ColorText() As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim rqstIdGrafico As Integer
        Dim rqstIdPagina As Integer
        If IsPostBack Then
            rqstIdGrafico = Request.Form("idGrafico")
        Else
            Try
                rqstIdGrafico = Request.QueryString("id").ToString
                rqstIdPagina = Request.QueryString("idPagina").ToString
            Catch exc As Exception
                rqstIdGrafico = 0
            End Try
        End If

        Try
            Dim cnx As SqlConnection = New SqlConnection(ConfigurationManager.AppSettings("strConexion"))

            Dim sql1 As String = "select a.id_ind, a.id_tipografico, a.color_grafico, a.maximo_izq_grafico, a.minimo_izq_grafico, " _
                & "a.maximo_der_grafico, a.minimo_der_grafico, a.etiquetas_vertical, a.periodos from indicador a, ordenindicador b  where a.id_ind = " & rqstIdGrafico
            Dim da1 As SqlDataAdapter = New SqlDataAdapter(sql1, cnx)
            Dim dt1 = New DataTable
            da1.Fill(dt1)

            Dim sql2 As String = "select distinct a.id_var, c.nombre, b.color from Variable a join IndicadorVariable b on a.id_var = b.id_var " _
                & " join NombreVar c on a.id_var = c.id_var where b.id_ind = '" & rqstIdGrafico & "'"
            Dim da2 As SqlDataAdapter = New SqlDataAdapter(sql2, cnx)
            Dim dt2 = New DataTable
            da2.Fill(dt2)

            Dim count = dt2.Rows.Count - 1
            ReDim TextBox(count)
            ReDim ColorText(count)
            For pos As Integer = 0 To count
                TextBox(pos) = New TextBox
                TextBox(pos).ID = "color_g-" & dt2.Rows(pos)("id_var")
                If Not IsDBNull(dt2.Rows(pos)("color")) Then
                    TextBox(pos).Text = dt2.Rows(pos)("color")
                End If
                ColorText(pos) = dt2.Rows(pos)("nombre")
            Next

            maximoI.Text = dt1.Rows(0)("maximo_izq_grafico").ToString().replace(",", ".")
            minimoI.Text = dt1.Rows(0)("minimo_izq_grafico").ToString().replace(",", ".")
            maximoD.Text = dt1.Rows(0)("maximo_der_grafico").ToString().replace(",", ".")
            minimoD.Text = dt1.Rows(0)("minimo_der_grafico").ToString().replace(",", ".")

            If Not IsDBNull(dt1.Rows(0)("etiquetas_vertical")) Then
                If dt1.Rows(0)("etiquetas_vertical") = 1 Then
                    etiquetasVertical.Checked = True
                End If
            End If


            periodos.Value = dt1.Rows(0)("periodos").ToString()
            idGrafico.Value = rqstIdGrafico
            idPagina.Value = rqstIdPagina
        Catch exc As Exception
            Response.Write(exc.Message & exc.ToString)
        End Try

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cnx As SqlConnection = New SqlConnection(ConfigurationManager.AppSettings("strConexion"))
        Dim color As String
        Dim a_color As ArrayList
        Dim cmd As SqlCommand
        
        Form.FindControl("maximo")
        If (IsPostBack) Then
            a_color = New ArrayList()
            For Each element As String In Request.Form
                If element.IndexOf("color_g") >= 0 Then
                    Dim valor = Request.Form(element)
                    If valor = "FFFFFF" Then
                        Dim a = element.ToString.Split("-")
                        Dim sql0 = "update indicadorvariable set color = NULL where id_var = " & a(1)
                        Response.Write(sql0)
                        cnx.Open()
                        cmd = New SqlCommand(sql0, cnx)
                        cmd.ExecuteNonQuery()
                        cnx.Close()
                    End If
                    If valor <> "" And valor <> "FFFFFF" Then
                        Dim a = element.ToString.Split("-")
                        Dim sql0 = "update indicadorvariable set color = '" & valor & "' where id_var = " & a(1)
                        Response.Write(sql0)
                        cnx.Open()
                        cmd = New SqlCommand(sql0, cnx)
                        cmd.ExecuteNonQuery()
                        cnx.Close()
                    End If
                End If
            Next

            Dim maximoI = Request.Form("maximoI").replace(",", ".")
            Dim minimoI = Request.Form("minimoI").replace(",", ".")
            Dim maximoD = Request.Form("maximoD").replace(",", ".")
            Dim minimoD = Request.Form("minimoD").replace(",", ".")
            Dim idPagina = Request.Form("idPagina")
            Dim periodos = Request.Form("periodos")
            Dim vertical = Request.Form("etiquetasVertical")
            Dim _vertical = 0
            If vertical = "on" Then
                _vertical = 1
            End If
            Try
                Dim sql1 As String = "Update indicador set color_grafico = '" & Join(a_color.ToArray, ",") & "', maximo_izq_grafico = '" & maximoI & "', " _
                    & "minimo_izq_grafico = '" & minimoI & "', maximo_der_grafico = '" & maximoD & "', minimo_der_grafico = '" & minimoD & "', periodos = '" & periodos & "', etiquetas_vertical = '" & _vertical & "' where id_ind = " & idGrafico.Value
                'Response.Write(sql1)
                cnx.Open()
                cmd = New SqlCommand(sql1, cnx)
                cmd.ExecuteNonQuery()
                cnx.Close()

                'Form the script that is to be registered at client side.
                Dim scriptString As String = "<script language=JavaScript>"
                scriptString += "window.opener.location.href=window.opener.location.pathname+'?id=" & idPagina & "&idGrafico=" & idGrafico.Value & "'; self.close()<"
                'scriptString += "window.opener.location.reload(); self.close()<"
                scriptString += "/"
                scriptString += "script>"
                If (Not IsClientScriptBlockRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", scriptString)
                End If
            Catch exc As Exception
                Response.Write(exc.Message & exc.ToString)
            End Try
        End If
    End Sub
End Class
